import { isCompanyPersonNoSlave, isCompanyPerson } from "@/api/index";
import { isCompanyStaff, checkPersonAudit, getPersonBindCompanyStatus } from "@/api/modules/company";

export default {
  data() {
    return {
      personId: uni.getStorageSync("person-id").slice(2) || '',
    }
  },
  methods: {
    checkCompanyStatus(isIndex) {
      if(!this.personId) {
        uni.switchTab({
           url: '/pages/index/index'
        })
        return;
      }
      let params = {
        person_id: this.personId,
      };
      // uni.navigateTo({
      //   url: '/pagesSub/company/recCard/index',
      // });
      // return
      // 流程判断
      isCompanyPerson(params).then(res => {
        if(res.info && res.info.state == 1) {
            uni.setStorageSync("company-id", res.info.company_id);
            getApp().globalData.companyState = '1';
            this.handlePass(isIndex);
        } else if(res.info && res.info.state == 0) {
            getApp().globalData.companyState = '';
            uni.setStorageSync("company-id", res.info.company_id);
            this.getFailInfo(res.info.company_id)
        } else {
            getApp().globalData.companyState = '';
            isCompanyPersonNoSlave(params).then(noSlaveRes => {
            if(noSlaveRes && noSlaveRes.code == 200) {
              uni.showModal({
                title: '提示',
                content: '您已开通云聘服务，无法使用小程序，详情请联系客服0755—86153516',
                showCancel:false,
                success: () => {}
              })
            } else {
              this.setIdentityData && this.setIdentityData()
              uni.hideLoading();
              uni.reLaunch({
                url: '/pagesSub/company/recCard/index',
              });
            }
          })

          // this.getIsCompanyStaff(isIndex)
        }
      });
    },
    getIsCompanyStaff(isIndex) {
      let params = {
        person_id: this.personId,
      };
      uni.showLoading();
      isCompanyStaff(params).then(res => {
        if (res && res.code == 200) {
          if (res.info && res.info.company_id) {
            uni.setStorageSync("company-id", res.info.company_id);
            if (res.info.state == 1) {
              this.handlePass(isIndex);
            } else {
              this.getFailInfo(res.info.company_id);
            }
          } else {
            this.setIdentityData && this.setIdentityData()
            uni.hideLoading();
            uni.reLaunch({
              url: '/pagesSub/company/recCard/index',
            });
          }
        } else if(res && res.code == 10) {
          this.setIdentityData && this.setIdentityData()
          uni.hideLoading();
          uni.reLaunch({
            url: '/pagesSub/company/recCard/index',
          });
        }  else {
          uni.hideLoading();
          uni.showToast({
            title: res.status_desc || '切换出错',
            icon: 'none',
            duration: 2000
          });
        }
      }).catch(res => {
        uni.hideLoading();
      });
    },
    // 查看认证状态结果
    getFailInfo(companyId) {
      // let params = {
      //   person_id: this.personId,
      //   company_id: companyId,
      // }
      // uni.showLoading();
      // getFailInfo(params).then(res => {
      //   uni.hideLoading();
      //   if (res.code == 101) { // 没有审核记录
      //     this.setIdentityData && this.setIdentityData()
      //     uni.reLaunch({
      //       url: '/pagesSub/company/selectCert/index',
      //     })
      //   } else if (res.code == 200 || res.code == 203) { // 审核中 || 审核失败
      //     this.setIdentityData && this.setIdentityData()
      //     uni.reLaunch({
      //       url: '/pagesSub/company/auditResults/index',
      //     });
      //   }
      // }).catch(res => {
      //   uni.hideLoading();
      // });
      let params = {
        personId: this.personId,
        companyId: companyId,
      };
      uni.showLoading();
      checkPersonAudit(params).then(res => {
        uni.hideLoading();
        if( res && (res.code == 202 || res.code == 203 || res.code == 204)) { //  202:审核中 || 203:营业执照审核失败 204：在职证明审核失败
          this.setIdentityData && this.setIdentityData()
          uni.reLaunch({
            url: '/pagesSub/company/auditResults/index',
          });
        } else if(res && res.code == 205) { // 需要上传在职证明
          this.setIdentityData && this.setIdentityData()
          uni.reLaunch({
            url: '/pagesSub/company/selectCert/index',
          })
        } else if(res && res.code == 206) { // 需要上传营业执照
          this.setIdentityData && this.setIdentityData()
          uni.reLaunch({
            url: '/pagesSub/company/selectCert/index?type=1',
          })
        }
      }).catch(res => {
          uni.hideLoading();
        });
    },
    handlePass(isIndex) {
      uni.hideLoading();
      if(isIndex) {
        return false;
      }
      this.setIdentityData && this.setIdentityData()
      uni.reLaunch({
        url: '/pages/library/index',
      });
    },

    // 获取人才绑定企业状态
    getPersonBindCompanyStatus() {
      const params = {
        personId: this.personId,
      };
      uni.showLoading();
      getPersonBindCompanyStatus(params).then(res => {
        if(res.code == 200) {
          // 1已绑定一览通体系 2旧账号体系 3未绑定 4绑定审核未通过 5绑定待审核
          getApp().globalData.companyBindType = res.data.type;
          if(res.data.type == 1) {
            getApp().globalData.companyVipLevel = res.data.newAccountSystemData.level;
            this.handlePass();
            uni.setStorageSync("company-id", res.data.newAccountSystemData.company_id);
            getApp().globalData.companyState = '1';
          }
          else if(res.data.type == 2) {
            uni.showModal({
              title: '提示',
              content: '您已开通云聘服务，无法使用小程序，详情请联系客服0755—86153516',
              showCancel:false,
              success: () => {}
            });
          } else if(res.data.type == 3) {
            getApp().globalData.companyVipLevel = res.data.noAccountSystemData.level;
            this.setIdentityData && this.setIdentityData();
            uni.reLaunch({
              url: '/pages/companyCenter/index',
            });
          } else if(res.data.type == 4 || res.data.type == 5) {

            // let auditData = res.data.auditData;
            this.handleToAuth(res.data.type, res.data.auditData)
          }
        } else {
          uni.showToast({
            title: res.status_desc,
            icon: 'none',
            duration: 2000
          });
        };
      }).finally(() => {
        uni.hideLoading();
      })
    },

    handleToAuth(companyBindType, auditData) {
      if(companyBindType == 3) {
        uni.reLaunch({
          url: '/pagesSub/company/recCard/index',
        });
      } else if(auditData.code == 202 || auditData.code == 203 || auditData.code == 204) {//  202:审核中 || 203:营业执照审核失败 204：在职证明审核失败
          this.setIdentityData && this.setIdentityData()
          uni.reLaunch({
          url: '/pagesSub/company/auditResults/index',
        });
      } else if(auditData.code == 205) { // 需要上传在职证明
        this.setIdentityData && this.setIdentityData()
        uni.reLaunch({
          url: '/pagesSub/company/selectCert/index',
        });
      } else if(auditData.code == 206) {// 需要上传营业执照
        this.setIdentityData && this.setIdentityData()
        uni.reLaunch({
          url: '/pagesSub/company/selectCert/index?type=1',
        })
      }
    },
  }
}